package com.resou.reader.base.p;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.support.annotation.NonNull;

import com.resou.reader.base.m.IModel;
import com.resou.reader.base.v.IView;
import com.resou.reader.utils.log.RsLog;
import com.resou.reader.utils.RxLifecycleUtils;
import com.uber.autodispose.AutoDisposeConverter;

/**
 * Created by Garrett on 2018/9/2.
 * contact me krouky@outlook.com
 */
public class BasePresenter<V extends IView, M extends IModel> implements IPresenter {

    protected V view;
    public M model;//这里public主要是为了跟activity生命周期同步

    private LifecycleOwner lifecycleOwner;
   public int pageSize = 15;

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        RsLog.d("BasePresenter onCreate()--->" + getClass().getSimpleName());
        lifecycleOwner = owner;
    }

    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        RsLog.d("BasePresenter onDestroy()--->" + getClass().getSimpleName());
        view = null;
        model = null;
    }

    @Override
    public void onLifecycleChanged(@NonNull LifecycleOwner owner, @NonNull Lifecycle.Event event) {

    }

    protected <T> AutoDisposeConverter<T> bindLifecycle() {
        if (null == lifecycleOwner)
            throw new NullPointerException("lifecycleOwner == null");
        return RxLifecycleUtils.bindLifecycle(lifecycleOwner);
    }
}
